package com.ssky.bean.validator.util;

import com.ssky.bean.validator.core.Constant;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
/**
 * @Author: ssky
 * @Date: 2021/07/18
 * @Description:
 */
public class ReadUtil {

  private static final ReadUtil ruleReader = new ReadUtil();
  
  public static ReadUtil getInstance() {
    return ruleReader;
  }
  
  public String readAll(InputStream inputStream) throws IOException {
    return readAll(inputStream, Constant.Charset_Utf8);
  }
  
  public String readAll(InputStream inputStream, Charset charset) throws IOException {
    StringBuilder vRuleStr = new StringBuilder("");
    if (inputStream == null) {
      return "";
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset));
    try {
      while (true) {
        String line = reader.readLine();
        if (line != null) {
          vRuleStr.append(line);
          continue;
        } 
        break;
      } 
    } catch (IOException e) {
      e.printStackTrace();
      throw e;
    } finally {
      if (null != reader)
        reader.close(); 
    }
    return vRuleStr.toString();
  }
}